Previous | Chapter contents | Next | Book PDF
NSDocument is a model-controller class. Its main job is to own and manage the model objects that make up a document and to provide a way of saving those objects to a file and reloading them later. Any and all objects that are part of the persistent state of a document should be considered part of that document's model. Sometimes the NSDocument itself has some data that would be considered part of the model. For example, the Sketch example application has a subclass of NSDocument subclass named Document; objects of this class might have an array of Graphic objects that comprises the model of the document. In addition to the actual Graphic objects, the Document object contains some data that should technically be considered part of the model since the order of the graphics within the document's array matters in determining the front-to-back ordering of the Graphics.
An NSDocument should not contain or require the presence of any objects that are specific to the application's user interface. Although a document can own and manage NSWindowController objects--which present the document visually and allow the user to edit it--it should not depend on these objects being there. For example, it might be desirable to have a document open in your application without having it visually displayed. For instance, a script might have opened a document to do some processing on it. If the script does not need the user to become involved in the processing, the script might want the document to be opened, manipulated, saved, and closed again, without it ever appearing onscreen.
Previous | Chapter contents | Next | Book PDF